

/*
 * ====================================
 * 聊天 chat
 */
$(function(){
	//$("#talkerPanel").slideToggle(500);
	initChat();
});

function initChat(){
	$(".choose-bar").click(function(){
		$(".roleList").slideToggle(function(){
			$(".choose-bar em").toggleClass("rotate");
		});
	});
	
	addTalker({name:iPadLang.allPeople});
	changeToTalker({name:iPadLang.allPeople});
	
}

function changeClass(){
	var thisRoleClass = $(this).attr("class");
	$(".choose-bar h3").attr("class",thisRoleClass);
	if(thisRoleClass == "role-all"){
		$(this).addClass("role-all-now");
		$(this).siblings().removeClass("role-all-now");
		$(this).siblings().removeClass("role-now");
	}else{
		$(this).addClass("role-now");
		$(this).siblings().removeClass("role-now");
		$(this).siblings().removeClass("role-all-now");
	}
}

var CHAT ={
	talkers:{},
	tipOnReceive:true
};

var iPadLang = iPadLang || {};


//{talkerId:"", receiverId:"",msg:""}
function receiveChat(chat){
	debug("receive chat:", chat);
	var talker = {id:chat.talkerId, name:chat.talkerName};
	if(isNewTalker(talker)){
		addTalker(talker);
	}
	appendChatMsg(chat);
	if(CHAT.tipOnReceive){
		appendChatTip();
		CHAT.tipOnReceive = false;
	}
	QA.newMyqaCount += 1;
	flushNewQaCount();
}

function sendChatMsg(){
	
}

function appendChatMsg(chat){
	var $Elem = createChatElem(chat);
	$("#qaMyList").append($Elem);
}

function appendChatTip(){
//	$("#qaMyList").append("");
}

function createChatElem(chat){
	var sign = CHAT.talkers[chat.talkerId].name;
	var msg = chat.msg||"";
	var sElem = '<dl class="chatMsg"><dd>'+ msg +'<p class="sign">- '+sign+'</p></dd></dl>';
	return $(sElem);
}

function isNewTalker(talker){
	if(CHAT.talkers[talker.id]){
		return false;
	}
	return true;
}

function addTalker(talker){
	var $elem = appendTalker(talker);
	CHAT.talkers[talker.id] = talker;
	$elem.bind("click", changeClass);
	$elem.bind("click", function(){changeToTalker({id:$(this).attr("name"), name:$(this).text()});});
}

function appendTalker(talker){
	var $elem = createTalkerElem(talker);
	$("#talkerContainer").append($elem);
	return $elem;
}

function createTalkerElem(talker){
	var className = "role-adm";
	if(isToAll(talker)){
		className = "role-all role-all-now";
	}
	var sElem = '<li class="'+className+'" name="'+talker.id+'">'+talker.name+'</li>';
	return $(sElem);
}

//
function changeToTalker(talker){
	var txt;
	if(isToAll(talker)){
		txt = Lang.placeholderFill(iPadLang.talktoBarLabel, [iPadLang.allPeople]);
		$("#talkToBar").removeAttr("name");
	}else{
		txt = Lang.placeholderFill(iPadLang.talktoBarLabel, [talker.name]);
		$("#talkToBar").attr("name", talker.id);
	}
	$("#talkToBar").text(txt);
}

//undefined时表示所有人
function currTalketo(){
	var talkto = $("#talkToBar").attr("name");
	return talkto;
}

function isToAll(talker){
	if(!talker.id || talker.id=="undefined"){
		return true;
	}
	return false;
}

function createMyChatSubmit(){
	var chat = {};
	chat.content = $.trim($("#qaText").val());
	chat.senderid = currTalketo();
	return chat;
}

function commitChat(talkto){
	if(!beforeQaSubmit())return false;
	var mychat = createMyChatSubmit();
	if(typeof submitChat == "function"){
		try{
			submitChat(mychat);//外部接口
			appendMyChat(mychat);
		}catch(e){
			console.error("[ipad-live][commitChat] submit exception:");
			console.error(e);
			return false;
		}
	}else{
		console.error("[ipad-live][commitChat] undefined: submitChat()!");
		return false;
	}
	$("#qaText").val("");
	return false;
}

function appendMyChat(mychat){
	var $Elem = createMyChatElem(mychat);
	$("#qaMyList").append($Elem);
}

function createMyChatElem(mychat){
	var msg = mychat.content;
	var talkerName = CHAT.talkers[mychat.senderid].name;
	var sElem = '<dl class="chatMsg"><dd>'+ msg +'<p class="sign">- <span style="font-style: italic;">to</span> '+talkerName+'</p></dd></dl>';
	return $(sElem);
}

